<?php
/**
 * Created by PhpStorm.
 * User: cc
 * Date: 2024/3/16
 * Time: 11:46
 */

namespace app\zyhsmarketing\controller;

use app\zyhsapi\model\marketing\ZyhsVivoMarketingData;
use think\Controller;
use think\Log;

class Vivo extends Controller
{
    public function getCode()
    {
        $param = request()->param();
        Log::write('vivo marketing get code: ' . json_encode($param, JSON_UNESCAPED_UNICODE), 'log', true);
    }

    /**
     * vivo营销点击检测
     * @return \think\response\Json
     */
    public function click()
    {
        $data = $this->request->param();
//{"0":{"adName":"联调测试广告名称","campaignId":3000,"ip":"103.90.176.228","groupId":1000,"clickTime":1710852195367,"mediaType":1,"ua":"Mozilla\/5.0 (Linux; Android 9; V1809T Build\/PKQ1.181030.001; wv) AppleWebKit\/537.36 (KHTML, like Gecko) Version\/4.0 Chrome\/68.0.3440.91 Mobile Safari\/537.36","creativeId":20000000,"advertiserName":"haozhunleji","advertiserId":"724cabfa09a1c20ecece","advertisementId":2000,"groupName":"联调测试广告组名称","adId":20000000,"requestId":"0000011111222_aaaaabbbbbcccccdddddeeeeefffffg","campaignName":"联调测试计划名称","oaid":"bb8192ba-df82-41b9-8652-6371855d17f7"},"target_type":"1","day":"30"}
//        Log::write('vivo marketing click: ' . json_encode($data, JSON_UNESCAPED_UNICODE), 'log', true);

        // vivo点击监测连接
        // HOST + ?os=_OS_&imei=_IMEI_&ip=_IP_&oaid=_OAID_&oaidplain=_OAIDPLAIN_&ua=_UA_&androidid=_ANDROIDID_&location_LOCATION_&requestid=_REQUESTID_&requestname=_REUQESTNAME_&advertiserid=_ADVERTISERID_&adname=_ADNAME_&creativeid=_CREATIEID_&osversion=_OSVERSION_&model=_MODEL_&lang=_LANG_&resolution=_RESOLUTION_&nettype=_NETTYPE_&ts=_TS_&rtaexp=_RTAEXP_
        try
        {
            $insert = [];
            foreach ($data as $datum)
            {
                $insert[] = [
                    'request_id' => $datum['requestId'] ?? '',
                    'advertiser_id' => $datum['advertiserId'] ?? '',
                    'campaign_id' => $datum['campaignId'] ?? '',
                    'creative_id' => $datum['creativeId'] ?? '',
                    'group_id' => $datum['groupId'] ?? '',
                    'advertisement_id' => $datum['advertisementId'] ?? '',
                    'oaid' => $datum['oaid'] ?? '',
                    'imei' => $datum['imei'] ?? '',
                    'ts' => $datum['clickTime'] ?? '',
                    'extend' => json_encode($datum, JSON_UNESCAPED_UNICODE),
                    'create_time' => $this->request->time()
                ];
            }
            (new ZyhsVivoMarketingData())->saveAll($insert);
        }
        catch (\Exception $e)
        {
            \think\Log::write(__CLASS__ . ' => ' . __FUNCTION__ . '()异常：' . $e->getMessage() . '失败信息：' . $e->getFile() . ':' . $e->getLine(), 'error', true);
            return show(-1, '操作失败');
        }
        return show(0, '操作成功');
    }
}
